package com.bclz.controller;


import java.util.Arrays;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bclz.bo.PostBo;
import com.bclz.common.response.ResponseData;
import com.bclz.common.response.ResponseUtils;
import com.bclz.service.PostService;
import com.bclz.util.EntityHelper;
import com.github.pagehelper.PageInfo;

import ch.qos.logback.classic.Logger;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
* Title: PostController  
* Description:  帖子Handle 
* @author xuchang  
* @date 2018年8月9日
 */
@RestController
@RequestMapping("/post")
public class PostController {
	
	
	private Logger log=(Logger)LoggerFactory.getLogger(getClass());
	
	@Autowired
	private PostService postService;
	
	
	/**
	 * 
	 * Title: listPost  
	 * Description:  查询帖子列表
	 * @param post
	 * @return
	 */
	@ApiOperation(value="查询帖子列表", notes="可选条件，标签Id、分类Id，标签名称模糊查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "categoryId", value = "分类Id", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "tagId", value = "标签Id", required = false, dataType = "String"),
		@ApiImplicitParam(name = "tags", value = "标签名称", required = false, dataType = "String"),
		@ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "Integer"),
		@ApiImplicitParam(name = "pageSize", value = "每页的数量", required = false, dataType = "Integer")}
	)
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ResponseData listPost(PostBo post) {
		try {
			PageInfo<PostBo> posts= postService.list(post);
			return ResponseUtils.success("查询成功", posts);
		}catch(Exception e){
			log.error("查询帖子列表失败",e.getMessage());
			return ResponseUtils.fail("查询失败", null);
		}
		
	}
	
	@ApiOperation(value="查询帖子", notes="根据Id查询帖子")
	@ApiImplicitParam(name = "id", value = "帖子Id", required = true, dataType = "String")
	@RequestMapping(value="/info/{id}",method=RequestMethod.GET)
	public ResponseData get(@PathVariable(value="id") String id) {
		try {
			PostBo post= postService.get(id);
			return ResponseUtils.success("查询成功", post);
		}catch(Exception e){
			log.error("查询帖子失败",e.getMessage());
			return ResponseUtils.fail("查询失败", null);
		}
		
	}
	
	
	/**
	 * 
	 * Title: sendPost  
	 * Description:  发帖
	 * @param post
	 * @return
	 */
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@ApiOperation(value="发布帖子", notes="")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "categoryId", value = "分类Id", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "content", value = "帖子内容", required = true, dataType = "String"),
		@ApiImplicitParam(name = "status", value = "状态", required = true, dataType = "Integer"),
		@ApiImplicitParam(name = "tags", value = "标签名称", required = true, dataType = "String"),
		@ApiImplicitParam(name = "title", value = "贴子标题", required = true, dataType = "String"),
		@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String")}
	)
	public ResponseData sendPost(PostBo post) {
		ResponseData res=EntityHelper.validatePost(post);
		if(res.getCode()==0) {
			return res;
		}	
		try {
			postService.save(post);;
			return ResponseUtils.success("保存成功", null);
		}catch(Exception e) {
			log.error("更新用户信息失败", e);
			return ResponseUtils.fail("保存失败，服务器内部错误，请稍后重试", null);
		}
		
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ResponseData deletePost(String ids) {
			try {
				postService.deleteById(Arrays.asList(ids.split(",")));
				return ResponseUtils.success("删除成功", null);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				log.error("删除失败", e);
				return ResponseUtils.fail("删除失败", null);
			}
		
		
	}
	
	
	
	
}
